package com.yunhe.rentcars.pro.service.permi.impl;


import com.yunhe.rentcars.commons.dto.Menu;
import com.yunhe.rentcars.pro.entity.permi.Permission;
import com.yunhe.rentcars.pro.service.permi.LoginService;
import com.yunhe.rentcars.pro.service.permi.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/16 0016.
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private PermissionService permissionService;
    @Override
    public List<Menu> leftMenu(Long roleId) {
        List<Menu> menuList = new ArrayList<>();
        Map<Long,Menu> menuMap = new HashMap<>();
        /*List<Permission> modulePermi = permissionService.findPermiMenuByRole(roleId, 1);
        List<Permission> pagePermi = permissionService.findPermiMenuByRole(roleId, 2);
        for(Permission p:modulePermi){
            Menu menu = new Menu(p.getId(),p.getName(),p.getUri());
            menuList.add(menu);
            menuMap.put(p.getId(),menu);
        }

        for(Permission p:pagePermi){
            Menu menu = new Menu(p.getId(),p.getName(),p.getUri());
            Menu parentMenu = menuMap.get(p.getParentId());
            if(null != parentMenu){
                parentMenu.getSubmenus().add(menu);
            }
        }*/
        return menuList;
    }


    @Override
    public List<Menu> findPermiMenu(Long roleId) {

       /* PermissionDao permissionDao = new DaoFactory<PermissionDao>().getDao(PermissionDao.class);*/
        Map map = new HashMap();
        map.put("roleId",roleId);
        List<Permission> permiList = permissionService.findPermisListByDuo(map);
                    List<Menu> menuList = new ArrayList<>();
                    for(Permission p : permiList){
                       if(p.getPermistype()==1){
                           Menu parentMenu = new Menu(p.getPermisid(),p.getPermisname(),p.getRelateurl());
                           for(Permission c : permiList){
                               if(c.getPermistype() == 2){
                                   if(null != c.getParentid() && c.getParentid().equals(p.getPermisid())){
                                       Menu pageMenu = new Menu(c.getPermisid(),c.getPermisname(),c.getRelateurl());
                                       parentMenu.getSubmenus().add(pageMenu);
                                   }
                               }
                           }
                           menuList.add(parentMenu);
                       }
                    }
             return   menuList;
    }



    @Override
    public Map<String, Permission> allPermission(Long roleId) {
        Map<String,Permission> permissionMap = new HashMap<>();
        Map map = new HashMap();
        map.put("roleId",roleId);
        List<Permission> allPermission = permissionService.findPermisListByDuo(map);

        for(Permission p : allPermission){
            permissionMap.put(p.getRelateurl(),p);
        }
        return permissionMap;
    }

    @Override
    public void autoLogin(HttpServletRequest req, HttpServletResponse resp, String autoLogin, String userName, String password){
        if(null != autoLogin && autoLogin.equals("自动登录")){
            Cookie cookie = new Cookie("userName",userName);
            cookie.setMaxAge(3600*24*7);
            cookie.setPath("/");
            resp.addCookie(cookie);

            Cookie cookie2 = new Cookie("password",password);
            cookie2.setMaxAge(3600*24*7);
            cookie2.setPath("/");
            resp.addCookie(cookie2);

        }else{
            Cookie[] cookies = req.getCookies();
            if(null != cookies){
                for(Cookie c : cookies){
                    if(c.getName().equals("userName")){
                        c.setMaxAge(0);
                    }
                    if(c.getName().equals("password")){
                        c.setMaxAge(0);
                    }
                }
            }
        }
    }
}
